<?php
namespace Backend\Controller;

use Think\Controller;

class PassportController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $site = F('site_cache');
        if(!$site)
        {
            D('settings')->updateSiteCache();
            $site = F('site_cache');
        }
        $this->assign('site_conf', $site);
    }
    //登陆
    public function login()
    {
        if(session('login_admin'))
        {
            $this->redirect('backend/index/index');
        }
        if(IS_AJAX){
            $name     = I('post.name');
            $password = I('post.password');
            $code     = I('post.code');

            //记录登录次数
            $check_num = D('admin')->checkloginnums();
            if($check_num) $this->ajaxReturn($check_num);

            if(!trim($code))
            {
                $msg = array('status' => 0, 'msg' => '请输入验证码');
                $this->ajaxReturn($msg);
            }
            if(!$this->check_verify($code, $id = ''))
            {
                $msg = array('status' => 0, 'msg' => '验证码错误，请重新输入');
                $this->ajaxReturn($msg);
            }

            //用户验证
            $admin   = D('admin')->checkLogin($name,$password);
            if(!$admin)
            {
                $msg = array('status' => 0, 'msg' => '用户名或密码错误');
               // $this->ajaxReturn($msg);
            }
            if($admin['islock'])
            {
                $msg = array('status' => 0, 'msg' => '帐户处于锁定状态');
                $this->ajaxReturn($msg);
            }
            if($admin['status'] != 1)
            {
                $msg = array('status' => 0, 'msg' => '帐户不可用');
                $this->ajaxReturn($msg);
            }

            //检测角色的合法性
            $role = D('admin_role')->alldisabled();
            if($role)
            {
                $role = i_array_column($role, 'id');
                session('permission_role', $role);
            }
            if(in_array($admin['role_id'], $role))
            {
                $msg = array('status' => 0, 'msg' => '用户的角色身份已被禁用');
                $this->ajaxReturn($msg);
            }

            //保存登陆信息 更新状态
            $save = D('admin')->updateLogin($admin['userid']);
            if(!$save)
            {
                $msg = array('status' => 0, 'msg' => '系统访问异常');
                $this->ajaxReturn($msg);
            }
            $login_info = array(
                'admin_id' => $admin['userid'],
                'role_id'  => $admin['role_id'],
                'role_name'=> M('admin_role')->where(array('id' =>$admin['role_id']))->getField('name'),
                'username' => $admin['username'],
                'nickname' => $admin['nickname'],
                'email'    => $admin['email'],
                'mobile'   => $admin['mobile'],
            );
            session('login_admin', $login_info);
            $msg = array('status' => 1, 'msg' => '身份核对完成，登录成功');
            $this->ajaxReturn($msg);
        }
        $this->assign('name', 'gywbank');
        $this->assign('version', time());
        $this->display();
    }

    //登陆验证码
    public function verifycode()
    {
        $config =    array(
            'fontSize'    =>    25,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    true, // 关闭验证码杂点
            'useCurve'    =>    false,
        );
        $Verify =     new \Think\Verify($config);
        $Verify->entry();
    }

    // 检测输入的验证码是否正确，$code为用户输入的验证码字符串
    function check_verify($code, $id = ''){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    //用户退出
    public function logout()
    {
        session('login_admin',null); // 删除login_admin
        $ip = get_client_ip();
        S('login_'.$ip,null);
        $this->redirect('/backend/passport/login');
    }
}